﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using NoticeHelper.Dingtalk.DingtalkCustomRobotSdk;
using System;
using System.Collections.Generic;
using System.Text;

namespace Helpers
{
    public static class NoticeHelperExtensions
    {
        public static IServiceCollection AddNoticeHelper(this IServiceCollection services, IConfiguration configuration, Action<NoticeOptions> configure = null)
        {
            var options = configuration.GetSection(NoticeOptions.SectionName).Get<NoticeOptions>() ?? new NoticeOptions();
            configure?.Invoke(options);
            NoticeOptions.Instance = options;

            if (options.UseEmail)
            {
                if (options.EmailConfig == null) throw new ArgumentNullException(nameof(options.EmailConfig));
                MailHelper.Init(options.EmailConfig);
            }

            if (options.UseDingTalk)
            {
                if (options.DingtalkOptions == null) throw new ArgumentNullException(nameof(options.DingtalkOptions));
                DingtalkClient.Init(options.DingtalkOptions.UrlBase, options.DingtalkOptions.Secret);
            }

            return services;
        }
    }
}
